package com.smile.controller;

import cn.hutool.core.util.ObjectUtil;
import com.smile.api.FlyApi;
import com.smile.api.PayloadApi;
import com.smile.commons.page.R;
import com.smile.model.dto.request.FlyRequestDTO;
import com.smile.model.dto.request.FlyStickControlRequestDTO;
import com.smile.model.dto.request.payload.CameraFocalLengthSetRequestDTO;
import com.smile.model.dto.request.payload.GimbalResetRequestDTO;
import com.smile.model.dto.request.payload.PayloadAuthorityGrabRequestDTO;
import com.smile.model.request.*;
import com.smile.model.response.response.GetFlyResultResponseDTO;
import com.smile.model.response.response.PayloadDataResponseDTO;
import com.smile.service.AirportService;
import com.smile.service.FlyService;
import com.smile.service.PayloadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/payload")
public class PayloadController {

    @Autowired
    private AirportService airportService;

    @Autowired
    FlyApi flyApi;

    @Autowired
    PayloadApi payloadApi;

    @Autowired
    PayloadService payloadService;

    /**
     * 负载控制
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/payloadAuthorityGrab")
    public R<Map<String, Object>> payloadAuthorityGrab(@RequestBody PayloadAuthorityGrabRequestDTO request) throws Exception {
        Map<String, Object> map = new HashMap<>();
        GetFlyResultResponseDTO flyResultResponseDTO = payloadService.payloadAuthorityGrab(request);
        if (flyResultResponseDTO.getStatus() == Boolean.TRUE) {
            map.put("message", "指令发送成功");
            map.put("errorCode",0 );
            map.put("flighttaskStepCode",5);
        }else{
            System.out.println(flyResultResponseDTO.getMessage());
            map.put("message", flyResultResponseDTO.getMessage());
            map.put("errorCode",flyResultResponseDTO.getCode());
        }
        return R.ok(map);
    }

    /**
     * 云台重置
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/gimbalReset")
    public R<Map<String, Object>> gimbalReset(@RequestBody GimbalResetRequestDTO request) throws Exception {
        Map<String, Object> map = new HashMap<>();
        GetFlyResultResponseDTO flyResultResponseDTO = payloadService.gimbalReset(request);
        if (flyResultResponseDTO.getStatus() == Boolean.TRUE) {
            map.put("message", "指令发送成功");
            map.put("errorCode",0 );
            map.put("flighttaskStepCode",5);
        }else{
            System.out.println(flyResultResponseDTO.getMessage());
            map.put("message", flyResultResponseDTO.getMessage());
            map.put("errorCode",flyResultResponseDTO.getCode());
        }
        return R.ok(map);
    }


    /**
     * 变焦
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/cameraFocalLengthSet")
    public R<Map<String, Object>> cameraFocalLengthSet(@RequestBody CameraFocalLengthSetRequestDTO request) throws Exception {
        Map<String, Object> map = new HashMap<>();
        GetFlyResultResponseDTO flyResultResponseDTO = payloadService.cameraFocalLengthSet(request);
        if (flyResultResponseDTO.getStatus() == Boolean.TRUE) {
            map.put("message", "指令发送成功");
            map.put("errorCode",0 );
            map.put("flighttaskStepCode",5);
        }else{
            System.out.println(flyResultResponseDTO.getMessage());
            map.put("message", flyResultResponseDTO.getMessage());
            map.put("errorCode",flyResultResponseDTO.getCode());
        }
        return R.ok(map);
    }

    /**
     * 画面拖动设置
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/cameraScreenDrag")
    public R<Map<String, Object>> cameraScreenDrag(@RequestBody CameraScreenDragRequest request) throws Exception {
        Map<String, Object> map = new HashMap<>();
        String payloadIndex = payloadService.getPayloadIndex(request.getGateway());
        request.setPayloadIndex(payloadIndex);
        System.out.println(request.getGateway());
        System.out.println(request);
        payloadApi.cameraScreenDrag(request);
        map.put("message", "指令发送成功");
        map.put("errorCode",0 );
        return R.ok(map);
    }

    /**
     * 直播镜头切换
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/liveLensChange")
    public R<Map<String, Object>> liveLensChange(@RequestBody LiveLensChangeRequest request) throws Exception {
        Map<String, Object> map = new HashMap<>();

        PayloadDataResponseDTO payloadDataResponseDTO =  payloadService.getPayloadData(request.getGateway());
        if(ObjectUtil.isNotNull(payloadDataResponseDTO)){
            request.setSn(payloadDataResponseDTO.getSn());
            flyApi.liveLensChange(request);
            map.put("message", "指令发送成功");
            map.put("errorCode",0 );
            return R.ok(map);
        }else{

            map.put("message", "未找到对应的负载");
            map.put("errorCode",1 );
            return R.ok(map);
        }
    }

    /**
     * 框选变焦
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/cameraFrameZoom")
    public R<Map<String, Object>> cameraFrameZoom(@RequestBody CameraFrameZoomRequest cameraFrameZoomRequest) throws Exception {
        Map<String, Object> map = new HashMap<>();

        PayloadDataResponseDTO payloadDataResponseDTO =  payloadService.getPayloadData(cameraFrameZoomRequest.getGateway());
        if(ObjectUtil.isNotNull(payloadDataResponseDTO)){
            cameraFrameZoomRequest.setPayloadIndex(String.valueOf(payloadDataResponseDTO.getPayloadIndex()));
            payloadApi.cameraFrameZoom(cameraFrameZoomRequest);
            map.put("message", "指令发送成功");
            map.put("errorCode",0 );
            return R.ok(map);
        }else{
            map.put("message", "未找到对应的负载");
            map.put("errorCode",1 );
            return R.ok(map);
        }
    }

}
